<template>
  <div class="dashboard">
    <!-- 顶部欢迎栏 -->
    <div class="header-bar">
      <h2>超市管理系统</h2>
      <div class="user-info">
        <span>欢迎您，{{ currentUser.userName }}</span>
        <el-button type="danger" link @click="handleLogout">
          <el-icon><SwitchButton /></el-icon>退出登录
        </el-button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-cards">
      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon user-icon">
            <el-icon><User /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-number">{{ userStats.total }}</div>
            <div class="stat-label">用户总数</div>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon employee-icon">
            <el-icon><Avatar /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-number">{{ employeeStats.total }}</div>
            <div class="stat-label">员工总数</div>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon active-icon">
            <el-icon><Check /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-number">{{ userStats.active }}</div>
            <div class="stat-label">活跃用户</div>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon working-icon">
            <el-icon><Briefcase /></el-icon>
          </div>
          <div class="stat-info">
            <div class="stat-number">{{ employeeStats.working }}</div>
            <div class="stat-label">在职员工</div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 快捷操作 -->
    <div class="quick-actions">
      <h3>快捷操作</h3>
      <div class="action-cards">
        <el-card class="action-card" shadow="hover" @click="goToUserManagement">
          <div class="action-content">
            <el-icon class="action-icon"><UserFilled /></el-icon>
            <div class="action-title">用户管理</div>
            <div class="action-desc">管理系统用户信息</div>
          </div>
        </el-card>

        <el-card class="action-card" shadow="hover" @click="goToEmployeeManagement">
          <div class="action-content">
            <el-icon class="action-icon"><Avatar /></el-icon>
            <div class="action-title">员工管理</div>
            <div class="action-desc">管理员工信息和状态</div>
          </div>
        </el-card>
      </div>
    </div>

    <!-- 系统信息 -->
    <div class="system-info">
      <el-card shadow="never">
        <template #header>
          <div class="card-header">
            <span>系统信息</span>
          </div>
        </template>
        <div class="info-grid">
          <div class="info-item">
            <span class="info-label">当前用户类型：</span>
            <el-tag :type="getUserTypeTagType(currentUser.userType)">
              {{ getUserTypeText(currentUser.userType) }}
            </el-tag>
          </div>
          <div class="info-item">
            <span class="info-label">登录状态：</span>
            <el-tag type="success">已登录</el-tag>
          </div>
          <div class="info-item">
            <span class="info-label">系统版本：</span>
            <span>v1.0.0</span>
          </div>
          <div class="info-item">
            <span class="info-label">最后登录：</span>
            <span>{{ formatDate(new Date()) }}</span>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  User, Avatar, Check, Briefcase, UserFilled, SwitchButton
} from '@element-plus/icons-vue'
import { getUserList } from '@/api/userApi'
import { getEmployeeList } from '@/api/employeeApi'

const router = useRouter()

// 获取当前登录用户信息
const currentUser = ref(JSON.parse(localStorage.getItem('user') || '{}'))

// 统计数据
const userStats = ref({
  total: 0,
  active: 0
})

const employeeStats = ref({
  total: 0,
  working: 0
})

// 获取统计数据
const fetchStats = async () => {
  try {
    // 获取用户统计
    const userRes = await getUserList()
    if (userRes.code === 200 && Array.isArray(userRes.data)) {
      userStats.value.total = userRes.data.length
      userStats.value.active = userRes.data.filter(user => user.status === '1').length
    }

    // 获取员工统计
    const employeeRes = await getEmployeeList()
    if (employeeRes.code === 200 && Array.isArray(employeeRes.data)) {
      employeeStats.value.total = employeeRes.data.length
      employeeStats.value.working = employeeRes.data.filter(emp => emp.status === '1').length
    }
  } catch (error) {
    console.error('获取统计数据失败:', error)
  }
}

// 跳转到用户管理
const goToUserManagement = () => {
  router.push('/user/list')
}

// 跳转到员工管理
const goToEmployeeManagement = () => {
  router.push('/employee/list')
}

// 退出登录
const handleLogout = () => {
  localStorage.removeItem('user')
  localStorage.removeItem('token')
  router.push('/login')
  ElMessage.success('已退出登录')
}

// 获取用户类型显示文本
const getUserTypeText = (userType) => {
  const typeMap = {
    'admin': '管理员',
    'employee': '员工',
    'user': '普通用户'
  }
  return typeMap[userType] || userType
}

// 获取用户类型标签颜色
const getUserTypeTagType = (userType) => {
  const typeMap = {
    'admin': 'danger',
    'employee': 'warning',
    'user': 'success'
  }
  return typeMap[userType] || 'info'
}

// 格式化日期
const formatDate = (date) => {
  return date.toLocaleString('zh-CN')
}

// 初始化
onMounted(() => {
  fetchStats()
})
</script>

<style scoped>
.dashboard {
  padding: 20px;
  background: #f5f7fa;
  min-height: calc(100vh - 40px);
}

.header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 0 20px;
  height: 60px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.header-bar h2 {
  margin: 0;
  font-size: 20px;
  color: #303133;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-info span {
  color: #606266;
}

.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.stat-card {
  border-radius: 8px;
  cursor: pointer;
  transition: transform 0.2s;
}

.stat-card:hover {
  transform: translateY(-2px);
}

.stat-content {
  display: flex;
  align-items: center;
  gap: 15px;
}

.stat-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
}

.user-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.employee-icon {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.active-icon {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.working-icon {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-info {
  flex: 1;
}

.stat-number {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-top: 5px;
}

.quick-actions {
  margin-bottom: 30px;
}

.quick-actions h3 {
  margin: 0 0 15px 0;
  color: #303133;
}

.action-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

.action-card {
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.action-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0,0,0,0.15);
}

.action-content {
  text-align: center;
  padding: 10px;
}

.action-icon {
  font-size: 32px;
  color: #409eff;
  margin-bottom: 10px;
}

.action-title {
  font-size: 16px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 5px;
}

.action-desc {
  font-size: 12px;
  color: #909399;
}

.system-info {
  margin-bottom: 20px;
}

.card-header {
  font-weight: bold;
  color: #303133;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 15px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.info-label {
  color: #606266;
  font-size: 14px;
}
</style>